package com.liming.server.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.liming.server.domain.Member;
import com.liming.server.domain.MemberExample;
import com.liming.server.dto.LoginMemberDto;
import com.liming.server.dto.MemberDto;
import com.liming.server.dto.PageDto;
import com.liming.server.exception.BusinessException;
import com.liming.server.exception.BusinessExceptionCode;
import com.liming.server.mapper.MemberMapper;
import com.liming.server.util.CopyUtil;
import com.liming.server.util.UuidUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class MemberService {

    private static final Logger LOG = LoggerFactory.getLogger(MemberService.class);

    @Resource
    private MemberMapper memberMapper;

    public void list(PageDto pageDto){
        PageHelper.startPage(pageDto.getPage(),pageDto.getSize());
        MemberExample memberExample = new MemberExample();
        List<Member> memberList = memberMapper.selectByExample(memberExample);
        PageInfo<Member> pageInfo = new PageInfo<>(memberList);
        pageDto.setTotal(pageInfo.getTotal());
        //将memberList通过CopyUtil.copyList()方法复制给memberDtoList
        List<MemberDto> memberDtoList =CopyUtil.copyList(memberList,MemberDto.class);
        pageDto.setList(memberDtoList);
    }

    public void save(MemberDto memberDto){
        //将memberDto通过CopyUtil.copy()方法复制给Member
        Member member = CopyUtil.copy(memberDto, Member.class);
        if(StringUtils.isEmpty(memberDto.getId())){
            this.insert(member);
        }else{
            this.update(member);
        }
    }

    private void insert(Member member){
        Date now = new Date();
        member.setId(UuidUtil.getShortUuid());
        member.setRegisterTime(now);
        memberMapper.insert(member);
    }
    private void update(Member member){
        memberMapper.updateByPrimaryKey(member);
    }
    public void delete(String id){
        memberMapper.deleteByPrimaryKey(id);
    }

    /**
     * 按手机号查找
     * @param mobile
     * @return
     */
    public Member selectByMobile(String mobile){
        if(StringUtils.isEmpty(mobile)){
            return null;
        }
        MemberExample example = new MemberExample();
        example.createCriteria().andMobileEqualTo(mobile);
        List<Member> memberList = memberMapper.selectByExample(example);
        if (memberList == null || memberList.size() == 0){
            return null;
        }else {
            return memberList.get(0);
        }
    }

    /**
     * 登录
     * @param memberDto
     * @return
     */
    public LoginMemberDto login(MemberDto memberDto){
        Member member = selectByMobile(memberDto.getMobile());
        if(member == null){
            LOG.info("手机号不存在, {}",memberDto.getMobile());
            throw new BusinessException(BusinessExceptionCode.LOGIN_MEMBER_ERROR);
        }else {
            if(member.getPassword().equals(memberDto.getPassword())){
                //登录成功
                LoginMemberDto loginMemberDto = CopyUtil.copy(member, LoginMemberDto.class);
                return loginMemberDto;
            }else {
                LOG.info("密码不对，输入密码:{}，数据库的密码:{}",memberDto.getPassword(),member.getPassword());
                throw new BusinessException(BusinessExceptionCode.LOGIN_MEMBER_ERROR);
            }
        }
    }

    /**
     * 查找手机号
     * @param mobile
     * @return
     */
    public MemberDto findByMobile(String mobile) {
        Member member = this.selectByMobile(mobile);
        return CopyUtil.copy(member,MemberDto.class);
    }

    /**
     * 重置密码
     * @param memberDto
     */
    public void resetPassword(MemberDto memberDto) {
        Member memberDb = this.selectByMobile(memberDto.getMobile());
        if(memberDb == null){
            throw new BusinessException(BusinessExceptionCode.MEMBER_NOT_EXIST);
        }else {
            Member member = CopyUtil.copy(memberDb,Member.class);
            member.setId(memberDb.getId());
            member.setPassword(memberDto.getPassword());
            memberMapper.updateByPrimaryKey(member);

        }
    }
}